package cn.jbooter.shiro.autoconfigure.filter.spring_session.mgt;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.Filter;
import org.apache.shiro.util.ClassUtils;
import org.apache.shiro.web.filter.authc.AnonymousFilter;
import cn.jbooter.shiro.autoconfigure.filter.spring_session.authc.SSRestAuthenticationFilter;
import cn.jbooter.shiro.autoconfigure.filter.spring_session.authc.SSRestLogoutFilter;
import cn.jbooter.shiro.autoconfigure.filter.spring_session.authc.SSRestUserFilter;
import cn.jbooter.shiro.autoconfigure.filter.spring_session.authz.SSRestPermissionsAuthorizationFilter;
import cn.jbooter.shiro.autoconfigure.filter.spring_session.authz.SSRestRolesAuthorizationFilter;

/**
 * 使用spring-session组件RestWebApplication的拦截器定义
 * 
 * @author hejian
 *
 */
public enum SpringSessionRestWebApplicationFilter {

	/** 匿名拦截器, 不需要登陆即可访问 **/
	ssr_anon(AnonymousFilter.class),
	/** 登录认证拦截器 **/
	ssr_authc(SSRestAuthenticationFilter.class),
	/** 登出拦截器 **/
	ssr_logout(SSRestLogoutFilter.class),
	/** 权限授权拦截器 **/
	ssr_perms(SSRestPermissionsAuthorizationFilter.class),
	/** 角色授权拦截器 **/
	ssr_roles(SSRestRolesAuthorizationFilter.class),
	/** 用户拦截器 **/
	ssr_user(SSRestUserFilter.class)
	;

	private final Class<? extends Filter> filterClass;

	private SpringSessionRestWebApplicationFilter(Class<? extends Filter> filterClass) {
		this.filterClass = filterClass;
	}

	public Filter newInstance() {
		return (Filter) ClassUtils.newInstance(this.filterClass);
	}

	public Class<? extends Filter> getFilterClass() {
		return this.filterClass;
	}
	
	/**
	 * 创建filter实例
	 * @return
	 */
	public static Map<String, Filter> createInstanceMap() {
        Map<String, Filter> filters = new LinkedHashMap<String, Filter>(values().length);
        for (SpringSessionRestWebApplicationFilter ssrFilter : values()) {
            Filter filter = ssrFilter.newInstance();
            filters.put(ssrFilter.name(), filter);
        }
        return filters;
    }
}
